package com.gm.wholesale.module.settlement.ui.main;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.gm.commonlib.base.baseBean.BaseFragmentInfo;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.order.adapter.ViewPagerAdapter;
import com.gm.wholesale.module.order.listener.MyOnPageChangeListener;
import com.gm.wholesale.module.settlement.bean.custorm.CustormReviceBean;
import com.gm.wholesale.module.settlement.ui.tab.FinishListFragment;
import com.gm.wholesale.module.settlement.ui.tab.UnPayListFragment;
import com.gm.wholesale.module.settlement.ui.tab.WaitSettlementFragment;
import com.gm.wholesale.module.settlement.ui.tab.WaitToAccuntListFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class AccountContentFragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    public ViewPagerAdapter mAdapter;

    private WaitToAccuntListFragment waitToAccuntListFragment;
    private UnPayListFragment unPayListFragment;
    private WaitSettlementFragment  waitSettlementFragment;
    private FinishListFragment finishListFragment;
    private CustormReviceBean custormReviceBean;
    private BaseGridFragment mCurrentFragment;
    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
       View view= LayoutInflater.from(context).inflate(R.layout.fragment_account_content, parent);
       initView(view);
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        int evenCode=event.getCode();

        if (evenCode== EventBusType.SHOW_CUSTORM_SETTLEMENT.getType()){
            custormReviceBean= (CustormReviceBean) event.getData();
            waitToAccuntListFragment.setCustomerId(custormReviceBean.getCustomerId());
            waitToAccuntListFragment.updateView();
            unPayListFragment.setCustomerId(custormReviceBean.getCustomerId());
            waitSettlementFragment.setCustomerId(custormReviceBean.getCustomerId());
            finishListFragment.setCustomerId(custormReviceBean.getCustomerId());
            mViewPager.setCurrentItem(0);
        }//刷新列表：修改了订单名字需要刷新，支付了需要刷新
        else if (evenCode==EventBusType.UPDATE_WAITTOACCUNTLIST.getType()){
            if (!(mCurrentFragment instanceof WaitToAccuntListFragment)){
                mCurrentFragment.refreshData();
            }
        }//取消未支付订单
        else if (evenCode==EventBusType.CANCLE_DZ_ORDER.getType()){
            mCurrentFragment.refreshData();
        }
    }



    private void initView(View view){
        mTabLayout = view.findViewById(R.id.tabLayout);
        mViewPager = view.findViewById(R.id.viewPager);

        mAdapter = getAdapter();
        //给ViewPager设置适配器
        mViewPager.setAdapter(mAdapter);
        mViewPager.setSaveEnabled(false);
        //将TabLayout和ViewPager关联起来
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new MyOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
               switch (position){
                   case 0:
                       mCurrentFragment=waitToAccuntListFragment;
                       waitToAccuntListFragment.updateView();
                       break;
                   case 1:
                       mCurrentFragment=unPayListFragment;
                       unPayListFragment.refreshData();
                       break;
                   case 2:
                       mCurrentFragment=waitSettlementFragment;
                       waitSettlementFragment.refreshData();
                       break;
                   case 3:
                       mCurrentFragment=finishListFragment;
                       finishListFragment.refreshData();
                       break;
               }
            }
        });

    }

    public ViewPagerAdapter getAdapter() {
        List<BaseFragmentInfo> datas = new ArrayList<>();
        //订单列表
        waitToAccuntListFragment=new WaitToAccuntListFragment();
        unPayListFragment=new UnPayListFragment();
        waitSettlementFragment=new WaitSettlementFragment();
        finishListFragment=new FinishListFragment();
        datas.add(new BaseFragmentInfo(1, "待对账", waitToAccuntListFragment));
        datas.add(new BaseFragmentInfo(2, "未支付", unPayListFragment));
        datas.add(new BaseFragmentInfo(3, "待结算", waitSettlementFragment));
        datas.add(new BaseFragmentInfo(4, "已完成", finishListFragment));

        mAdapter = new ViewPagerAdapter(getChildFragmentManager(), datas);
        return mAdapter;
    }


    private void removeFragment(Fragment fragment) {
        getChildFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //todo 必须销毁它嵌套的相关fragment，不然下次添加会报错
        // todo因为子fragment没有销毁，还依附在当前fragment，不能再次被添加
        removeFragment(waitSettlementFragment); //移除嵌套的fragment
        removeFragment(unPayListFragment); //移除嵌套的fragment
        removeFragment(finishListFragment); //移除嵌套的fragment
        removeFragment(waitToAccuntListFragment); //移除嵌套的fragment
        waitSettlementFragment=null;
        unPayListFragment=null;
        finishListFragment=null;
        waitToAccuntListFragment=null;
    }
}
